package com.jinxin.platform.cdsmsmgr.rpc;

import com.alibaba.fastjson.JSON;
import com.jinxin.platform.base.api.annotation.*;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.base.common.pojo.InvokeCfg;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.BusinessDelegate;
import com.jinxin.platform.cdsmsmgr.pojo.entity.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * @description: (描述...)
 * @return:
 * @author: Linn
 * @time: 2020/05/18
 */
@Component
@Slf4j
@NeedInf(moduleKey = Constant.MODULE_KEY, className = Constant.MODULE_CLASS_NAME)
public class SysUserRpc {

    @Autowired
    private BusinessDelegate businessDelegate;

    /**
     * 获取当前用户对象
     *
     * @return SysUser
     */
    @APCOSApiOperation(value = "getCurrentUser", description = "获取当前登录者", infDict = "获取当前登录者",
            reqDemo = "{}", respDemo = "{\"userId\":\"123\",\"userName\":\"张三\",\"userAccount\":\"13812345678\"}",
            infType = APCOSApiConstans.INF_TYPE_OTHER,developers = "lingnan")
    @APCOSApiRequests({})
    @APCOSApiResponses({
            @APCOSApiResponse(name = "userAccount", value = "登录账户", dict = "登录账户",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "登录账户"),
            @APCOSApiResponse(name = "userId", value = "用户ID", dict = "用户ID",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户ID"),
            @APCOSApiResponse(name = "userName", value = "用户姓名", dict = "用户姓名",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户姓名"),
    })
    public SysUserVO getCurrentUser() {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(Constant.MODULE_KEY);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName("getCurrentUser");
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            log.info("getCurrentUser:" + result.getData());
            return JSON.parseObject(toJSONString(result.getData()), SysUserVO.class);
        }
        else {
            SysUserVO userVO = new SysUserVO();
            userVO.setUserId("1");
            userVO.setUserAccount("admin");
            userVO.setUserName("王麻子");
            return userVO;
        }
//        return null;
    }

    /**
     * 获取短信业务模块列表
     * @param dictType 字典类型
     * @return
     */
    @APCOSApiOperation(value = "getSmsBusinessList", description = "获取短信业务模块列表", infDict = "根据字典类型查询字典项",
            reqDemo = "{\"dictType\": \"smsBusinessType\"}", respDemo = "{}",
            infType = APCOSApiConstans.INF_TYPE_OTHER,developers = "lingnan")
    @APCOSApiRequests({
            @APCOSApiRequest(name = "dictType", value = "数据字典类型", dict = "字典类型",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,required = true,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "100",remark = "数据字典类型,例如短信业务字典smsType"),
    })
    public JsonResult getSmsBusinessList(String dictType) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(Constant.MODULE_KEY);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName("getSmsBusinessList");
        HashMap<String, String> map = new HashMap<>();
        map.put("dictType", dictType);
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        log.info("getSmsBusinessList =====>" + result.toString());
        if(result.getSuccess()){
            return result;
        }
        return null;
    }

    private JsonResult getJsonResult(String keyName,String funName) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(keyName);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName(funName);
        return businessDelegate.invoke(cfg);
    }


}
